package org.imast.services;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.imast.interfaces.InterfaceRequest;
import org.imast.models.ModelRequest;
import org.imast.utils.UtilHibernate;

public class ServiceRequest extends UtilHibernate implements InterfaceRequest{

	@Override
	public void save(ModelRequest request) {
		
		Session session = UtilHibernate.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		
		try {
			
			session.saveOrUpdate(request);
			session.getTransaction().commit();
			
		} catch (Exception e) {
			
			session.getTransaction().rollback();
			e.printStackTrace();
			
		}
		
	}

	@Override
	public void delete(ModelRequest request) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ModelRequest find(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ModelRequest> getAll() {
		
		Session session = UtilHibernate.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		List<ModelRequest> pedidos = null;
		
		try {
			
			pedidos = (List<ModelRequest>) session.createQuery("from ModelRequest").list();
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			session.getTransaction().rollback();
			e.printStackTrace();
			
		}
		
		return pedidos;
		
	}

}
